<?php

declare(strict_types=1);

namespace MultiWorker\Logger;

use MultiWorker\Constract\LoggerInterface;
use MultiWorker\Exception\LoggerChannelException;

class Logger implements LoggerInterface
{
    /**
     * logger channel.
     *
     * @var Channel
     */
    protected $channel;

    /**
     * logger config.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Logger Constructor.
     *
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->config = array_merge($this->config,$config);    
    }

    /**
     * new logger instance,
     *
     * @param array $arguments
     * @return Logger
     */
    public static function new (...$arguments): Logger
    {
        return new static(...$arguments);
    }

     /**
     * System is unusable.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function emergency($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Action must be taken immediately.
     *
     * Example: Entire website down, database unavailable, etc. This should
     * trigger the SMS alerts and wake you up.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function alert($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Critical conditions.
     *
     * Example: Application component unavailable, unexpected exception.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function critical($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Runtime errors that do not require immediate action but should typically
     * be logged and monitored.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function error($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Exceptional occurrences that are not errors.
     *
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
     * that are not necessarily wrong.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function warning($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Normal but significant events.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function notice($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Interesting events.
     *
     * Example: User logs in, SQL logs.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function info($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Detailed debug information.
     *
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function debug($message, array $context = [])
    {
        $this->log(__FUNCTION__, $message, $context);
    }

    /**
     * Logs with an arbitrary level.
     *
     * @param mixed  $level
     * @param string $message
     * @param array  $context
     *
     * @return void
     */
    public function log($level, $message, array $context = [])
    {
        $this->getChannel()->write($message, $level, $context);
    }
    
    /**
     * get logger channel instance.
     *
     * @return Channel
     */
    public function getChannel (): Channel
    {
        if (!$this->channel) {
            $channel = $this->config['channel'] ?? 'console';
            $classNames = ['MultiWorker\Logger\Channel\\' . ucfirst($channel),$channel];
            foreach ($classNames as $className) {
                if (class_exists($className)) {
                    $this->channel = new $className($this->config);
                    return $this->channel;
                }
            }
            throw new LoggerChannelException($channel . ' channel does not exist.');
        }
        return $this->channel;
    }

    /**
     * Logger __call.
     *
     * @param string $method
     * @param array $parameters
     * @return void
     */
    public function __call($method, $parameters)
    {
        $this->log($method, ...$parameters);
    }
}
